package ils.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import ils.domain.catalog.model.Book;
import ils.services.CatalogService;
import ils.services.SearchService;
import ils.util.SpringUtil;
import org.apache.struts2.interceptor.SessionAware;

import java.util.List;
import java.util.Map;

public class SearchAction extends ActionSupport implements SessionAware, Preparable
{
    private Map<String, Object> session;
    private String type;
    private String term;
    private List<Book> books;
    private CatalogService catalogService;
    
    public String execute() throws Exception
    {
        if(type == null) type = "subject";
        if(type.equals("category"))
        {
            books = catalogService.getBooksInCategory(term, true);
        }else
        {
            books = catalogService.searchBooks(type, term);
        }
        return SUCCESS;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public String getTerm()
    {
        return term;
    }

    public void setTerm(String term)
    {
        this.term = term;
    }

    public List<Book> getBooks()
    {
        return books;
    }

    public void setBooks(List<Book> books)
    {
        this.books = books;
    }

    public Map<String, Object> getSession()
    {
        return session;
    }

    @Override
    public void setSession(Map<String, Object> arg0)
    {
        this.session = arg0;
    }

    public void setCatalogService(CatalogService catalogService)
    {
        this.catalogService = catalogService;
    }

    @Override
    public void prepare() throws Exception {
        catalogService = (CatalogService) SpringUtil.getApplicationContext().getBean("catalogService");
    }
}
